﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for RoleDAO
/// </summary>
public class RoleDAO:DBAccess
{
    string SQL_GET_ROLES_NAME = "select * from [Roles] where roleID = @roleID";
    string SQL_GET_ROLES_NAME_By_UserID = "SELECT r.RoleName FROM Roles AS r JOIN Accounts as a ON r.RoleID = a.RoleID where a.Username = @userID";
    string message;
	public RoleDAO()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public string GetRoles(int roleID)
    {
        string roleName="";
        try
        {
            SqlCommand command = new SqlCommand(SQL_GET_ROLES_NAME, conn);
            command.Parameters.AddWithValue("@roleID", roleID);
            if (Connect())
            {
                SqlDataReader dr = command.ExecuteReader();
                while (dr.Read())
                {
                    roleName = dr.GetString(1);
                    break;
                }
                Disconnect();
            }
        }
        catch (SqlException ex) { message = ex.Message; }
        catch (Exception ex) { message = ex.Message; }
        return roleName;
    }

    public string GetRoles(string userID)
    {
        string roleName = "";
        try
        {
            SqlCommand command = new SqlCommand(SQL_GET_ROLES_NAME_By_UserID, conn);
            command.Parameters.AddWithValue("@userID", userID);
            if (Connect())
            {
                SqlDataReader dr = command.ExecuteReader();
                while (dr.Read())
                {
                    roleName = dr.GetString(0);
                    break;
                }
                Disconnect();
            }
        }
        catch (SqlException ex) { message = ex.Message; }
        catch (Exception ex) { message = ex.Message; }
        return roleName;
    }
    

}